home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 249 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.3 KB

  1. Path: rowdy.lonestar.org!nemesis!hammy!not-for-mail
  2. From: gordon@hammy.lonestar.org (Gordon Burditt)
  3. Newsgroups: comp.std.c
  4. Subject: Re: atexit() and return from main()
  5. Date: 30 Jan 1996 22:13:41 -0600
  6. Organization: What organization?
  7. Message-ID: <4emq9l$2fn@hammy.lonestar.org>
  8. References: <4dhfja$j50@fg70.rz.uni-karlsruhe.de> <4diicg$i9e@lyra.csx.cam.ac.uk> <TANMOY.96Jan22084309@qcd.lanl.gov> <DLMLL6.A0H@ukpsshp1.serigate.philips.nl>
  9. NNTP-Posting-Host: localhost.hammy.lonestar.org
  10.  
  11. >The question I am asking is if:
  12. >    main() calls setjmp() and then calls exit().  An atexit() function
  13. >    calls longjmp() and returns to main().  main() then continues to
  14. >    execute other code. Is the execution of this code defined behaviour
  15. >    (so long as no attempt is made to exit again)?
  16. ...
  17. ANSI C Classic 4.6.2.1 The longjmp function
  18.  
  19. The longjmp function restores the environment saved by the most recent
  20. invocation of the setjmp macro in the same invocation of the program, 
  21. with the corresponding jmp_buf argument.  If there has been no such
  22. invocation, or IF THE FUNCTION CONTAINING THE INVOCATION OF THE SETJMP
  23. MACRO HAS TERMINATED EXECUTION in the interim, the behavior is
  24. undefined.
  25. ...
  26. ANSI C Classic 2.1.2.2.3 Program Termination
  27. A RETURN FROM THE INITIAL CALL TO THE MAIN FUNCTION IS EQUIVALENT
  28. TO CALLING THE EXIT FUNCTION with the value returned by the main
  29. function as its argument.
  30. ...
  31. I claim that longjmp()ing out of an atexit function into main
  32. causes undefined behavior because main() has already returned
  33. (or, equivalently, called exit()) between the setjmp and
  34. longjmp calls.
  35.  
  36. In fact, calling exit() may not unwind the stack like returning
  37. from main() does, so you might get away with it, but there's
  38. no guarantee.
  39.  
  40. >A related question - does undefined behaviour invalidate (make undefined) all 
  41. >defined behaviour that has gone before? If so does this mean that any program 
  42. >that does not exit() or return from main have any defined behaviour?
  43.  
  44. Undefined behavior can do anything, including causing time to stop or
  45. make it go backwards.  It could even transport you into the Star
  46. Trek universe, where it's well established that cause need not
  47. precede effect.  In the words of Miles O'Brien:  "I hate temporal 
  48. mechanics".
  49.  
  50.                         Gordon L. Burditt
  51.                         sneaky.lonestar.org!gordon
  52.